package com.example.demo.gateway.controller;

import com.example.demo.gateway.entity.GatewayUpstream;
import com.example.demo.gateway.req.GatewayUpstreamReq;
import com.example.demo.gateway.service.GatewayUpstreamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/gateway/upstream")
public class GatewayUpstreamController {

    @Autowired
    private GatewayUpstreamService gatewayUpstreamService;

    @GetMapping("/list")
    public Flux<GatewayUpstream> findAll() {
        return gatewayUpstreamService.findAll();
    }

    @GetMapping("/{id}")
    public Mono<GatewayUpstream> findById(@PathVariable Long id) {
        return gatewayUpstreamService.findById(id);
    }

    @PostMapping
    public Mono<GatewayUpstream> add(@Valid  @RequestBody GatewayUpstreamReq gatewayUpstreamReq) {
        //参数校验
        return gatewayUpstreamService.add(gatewayUpstreamReq);
    }

    @PutMapping("/{id}")
    public Mono<GatewayUpstream> update(@PathVariable Long id, @RequestBody GatewayUpstreamReq gatewayUpstreamReq) {
        gatewayUpstreamReq.setId(id);
        log.info("正在保存");
        return gatewayUpstreamService.update(gatewayUpstreamReq);
    }
    @DeleteMapping("/{id}")
    public Mono<ResponseEntity<Void>> delete(@PathVariable Long id) {
        return gatewayUpstreamService.deleteById(id);
    }

    @GetMapping("/page")
    public Flux<GatewayUpstream> findPage(Integer pageNo, Integer pageSize) {
        return gatewayUpstreamService.findPage(pageNo, pageSize);
    }
    @GetMapping("/count")
    public Mono<Long> findCount() {
        return gatewayUpstreamService.count();
    }
}
